package com.jwdemo
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;

	//

	public final class GameSounds
	{
		
		private static var sfxSndTrans:SoundTransform 	=	new SoundTransform(0.01) ;
		private static var musicSndTrans:SoundTransform =	new SoundTransform(1) ;
		
		[Embed(source="../assets/demoloop.mp3")] static private var GameSoundsDemoLoop:Class ;
		static public var demoLoopSND:Sound 			= 	new GameSoundsDemoLoop() as Sound;

		[Embed(source="../assets/fire.mp3")] static private var GameSoundsFire:Class ;
		static public var fireSND:Sound 				= 	new GameSoundsFire() as Sound;

		public function GameSounds()
		{
		}

		static private var snds:Object = {
			backgroundmusic: 	demoLoopSND,
			fire: 				fireSND 		
		} ;
		
		static public function GetSound(name:String):Sound 
		{
			return GameSounds.snds[name] ;			
		}

		static public function PlaySFX(name:String):SoundChannel 
		{
			return Play(name, sfxSndTrans, 0, 0) ;		
		}

		static public function PlayMusic(name:String,loop:Boolean = false) : SoundChannel
		{
			// looping - fudge - Replaying sound on SOUND_COMPLETE event does not work!
			return Play(name, musicSndTrans, 0, loop ? 10000  : 0) ;		
		}
		
		static private function Play(name:String,sndTrans:SoundTransform,start:Number = 0, repeat:Number = 0):SoundChannel 
		{
			var snd:Sound 				= 	GameSounds.GetSound(name) ;
			if (snd) {
				var channel:SoundChannel	=	snd.play(start,repeat,sndTrans) ;
				channel.addEventListener(Event.SOUND_COMPLETE, function(event:Event):void {}) ;
				return channel ;
			} else
			{
				return null ;		
			}
		}

		
	}
	

}